package com.zhuhanpeng.cloud.role.control;

import com.alibaba.fastjson.JSON;
import com.zhuhanpeng.cloud.mybatis.entity.PtRoleEntity;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PadingRstType;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PagingEntity;
import com.zhuhanpeng.cloud.mybatis.ztree.ZtreeEntity;
import com.zhuhanpeng.cloud.role.entity.SearchRoleEntity;
import com.zhuhanpeng.cloud.role.service.RoleService;
import com.zhuhanpeng.cloud.spring.control.BaseController;
import com.zhuhanpeng.cloud.user.entity.SearchUserEntity;
import com.zhuhanpeng.cloud.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {
    private Logger logger = LogManager.getLogger(RoleController.class.getName());

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;
    @Secured("ROLE_cldk_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/queryRoleListByPage")
    @ResponseBody
    public PadingRstType<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingEntity paging){
        logger.info(entity);
        logger.info(paging);
        PadingRstType<PtRoleEntity> padingRstType = roleService.queryRoleListByPage(entity,paging);
        return padingRstType;
    }
    @RequestMapping("/queryZtreeByRoleId")
    @ResponseBody
    public List<ZtreeEntity> queryZtreeByRoleId(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid:"+roleUuid);
        List<ZtreeEntity> list = roleService.queryZtreeByRoleId(roleUuid);
        return list;
    }
    @RequestMapping("/saveMenuRefRole")
    @ResponseBody
    public String saveMenuRefRole(@RequestParam("menuIdArray")String menuIdArray,@RequestParam("roleUuidArray")String roleUuidArray){
        logger.info("menuIdArray:" +menuIdArray+ "roleUuidArray:" +roleUuidArray);
        roleService.saveMenuRefRole(menuIdArray,roleUuidArray);

        return sucessJson("role.assign.menu.success");

    }
    @RequestMapping("/queryMenuZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryMenuZtreeAll(){
        List<ZtreeEntity> list = roleService.queryMenuZtreeAll();
        return list;
    }

    @RequestMapping("/addroleInfoById")
    @ResponseBody
    public String addroleInfoById(PtRoleEntity entity) {
        logger.info(entity);
        roleService.addroleInfoById(entity);
        return sucessJson("role.add.info.success");
    }

    @RequestMapping("/deleteRoleInfo")
    @ResponseBody
    public String deleteRoleInfo(String roleUuid){
        roleService.deleteRoleInfo(roleUuid);
        return sucessJson("role.delete.info.success");
    }

    @RequestMapping("/queryUserInfoByRole")
    public String queryUserInfoByRole(@RequestParam("roleUuid") String roleUuid, HttpServletRequest request) {
        logger.info("roleUuid:" + roleUuid);
        request.getSession().setAttribute("roleUuid", roleUuid);
        return "role/userDetail";
    }

    @RequestMapping("/queryRoleByRoleId")
    @ResponseBody
    public String queryRoleByRoleId(@RequestParam("roleUuid") String roleUuid) {
        logger.info("userUuid:" + roleUuid);
        PtRoleEntity ptRoleEntity = roleService.queryRoleByRoleId(roleUuid);
        return JSON.toJSONString(ptRoleEntity);
    }

    @RequestMapping("/updateRoleInfoById")
    @ResponseBody
    public String updateRoleInfoById(PtRoleEntity entity) {
        logger.info("entity:" + entity);
        roleService.updateRoleInfoById(entity);
        return sucessJson("update.role.info.success");
    }

//    @RequestMapping("/getUserInfolistByPage")
//    @ResponseBody
//    public PadingRstType<PtUserEntity> getUserInfolistByPage(SearchUserEntity search, PagingEntity pagingEntity) {
//        logger.info(search);
//        logger.info( pagingEntity);
//        PadingRstType<PtUserEntity> padingRstType = userService.getUserInfolistByPage(search, pagingEntity);
//        return padingRstType;
//    }

}